package com.fare.taluo.core.domain.system.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.dayatang.hibernate.EntityRepositoryHibernate;
import com.fare.taluo.core.domain.system.VersionInfoEntity;
import com.fare.taluo.core.domain.system.VersionInfoEntityReposity;

@Repository
public class VersionInfoEntityRepositoryImpl extends EntityRepositoryHibernate
		implements VersionInfoEntityReposity {

	public List<VersionInfoEntity> findVersionInfo(Long versionid) {
		List<Object> params = new ArrayList<Object>();
		String sql = "from VersionInfoEntity v where 1=1  ";
		if (versionid != null) {
			sql += "and v.id=?";
			params.add(versionid);
		}

		sql += "order by v.publishTime desc";

		List<VersionInfoEntity> versioninfoList = super.find(sql,
				params.toArray(), VersionInfoEntity.class);
		return versioninfoList;
	}

	public List<VersionInfoEntity> findAllEntities() {
		List<Object> params = new ArrayList<Object>();
		List<VersionInfoEntity> versionInfoEntities = super
				.find("from VersionInfoEntity v where v.flgDeleted=0 order by v.publishTime desc",
						params.toArray(), VersionInfoEntity.class);
		return versionInfoEntities;
	}

	public void saveOrUpdate(VersionInfoEntity versionInfoEntity) {
		super.save(versionInfoEntity);

	}

	public VersionInfoEntity getMaxVersion() {
		String hql = "from VersionInfoEntity v where v.versionNumber=(select max(v1.versionNumber) from VersionInfoEntity v1)";
		return super.getSingleResult(hql, new Object[]{}, VersionInfoEntity.class);
	}

}
